<?php

declare(strict_types=1);

namespace app\index\logic\traffic;

use app\index\logic\BaseLogic;
use app\common\service\third_party_api\ShipApiService;
use app\common\service\DateService;
use think\facade\Db;

/**
 * 船舶查询逻辑层
 * 处理船舶航线和航班相关的业务逻辑
 */
class ShipLogic extends BaseLogic
{
    /**
     * 获取航线信息
     * @param array $param 请求参数
     * @return array 航线和航班信息
     */
    public function getShipLine(array $param)
    {
        // 传了lineid用lineid查，没传用FromCityName和ToCityName查
        if (isset($param['lineId'])) {
            $shipline = Db::name('ship_line')
                ->where(['line_id' => $param['lineId']])
                ->select()
                ->toArray();
        } else {
            $shipline = Db::name('ship_line')
                ->where(['from_city_name' => $param['FromCityName'], 'to_city_name' => $param['ToCityName']])
                ->select()
                ->toArray();
        }

        if (!$shipline) {
            return [];
        } else {
            return [
                'shipline' => $shipline
            ];
        }
    }

    /**
     * 获取航班信息
     * @param array $param 请求参数
     * @return array 航班信息列表
     */
    public function getShipFlight(array $param)
    {
        $body_arr = [
            'LineId' => $param['lineId'],
            'SailDate' => $param['sailDate'],
        ];

        $shipApiService = new ShipApiService();
        $shipLineData = $shipApiService->getShipVoyagesV2($body_arr);

        $voyages = [];
        if ($shipLineData['status'] == 1) {
            $voyages = $shipLineData['data'];
        }

        if (!empty($voyages)) {
            foreach ($voyages as &$voyage) {
                $voyage['DepartTime'] = date('H:i', strtotime($voyage['DepartTime']));
                $voyage['ArriveTime'] = date('H:i', strtotime($voyage['ArriveTime']));
                $voyage['duration'] = DateService::getTimeHour(strtotime($voyage['ArriveTime']) - strtotime($voyage['DepartTime']));
                $voyage['sale_num'] = $this->getSeats($voyage['Seats']);
            }
        }

        return $voyages;
    }

    /**
     * 获取座位数
     * @param array $Seats 座位数据
     * @return int 总座位数
     */
    public function getSeats(array $Seats): int
    {
        $num = 0;
        foreach ($Seats as $seat) {
            $num += $seat['TicketLeft'];
        }
        return $num;
    }

    /**
     * 获取可改签的航班信息
     * @param array $param 请求参数，包含订单ID、航线ID等信息
     * @return array 可改签的航班信息列表
     */
    public function getChangeableShipFlights(array $param)
    {
        // 验证必要参数
        if (empty($param['orderId']) || empty($param['date'])) {
            return [];
        }

        // 构建请求参数
        $body_arr = [
            'orderSerialId' => $param['orderId'], //同程订单流水号
            'date' => $param['date'], // 改签发船日期：格式为yyyy-MM-dd
        ];

        // 调用API获取可改签航班
        $shipApiService = new ShipApiService();

        // 先验证当前订单是否可以改签
        $changeableShipVoyages = $shipApiService->checkChangeAble($body_arr);
        if ($changeableShipVoyages['status'] != 1) {
            return [];
        }

        // 获取可改签航班信息
        $shipLineData = $shipApiService->queryChangeVoyages($body_arr);

        $voyages = [];
        if ($shipLineData['status'] == 1) {
            $voyages = $shipLineData['data'];
        }

        // 处理航班数据
        if (!empty($voyages)) {
            foreach ($voyages as &$voyage) {
                $voyage['DepartTime'] = date('H:i', strtotime($voyage['DepartTime']));
                $voyage['ArriveTime'] = date('H:i', strtotime($voyage['ArriveTime']));
                $voyage['duration'] = DateService::getTimeHour(strtotime($voyage['ArriveTime']) - strtotime($voyage['DepartTime']));
                $voyage['sale_num'] = $this->getSeats($voyage['Seats']);
                // 标记为可改签航班
                $voyage['changeable'] = true;
            }
        }

        return $voyages;
    }
}
